package com.example.fengyungo.domain.pojo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * 管理员
 */
public class LoginUser implements UserDetails {

    /**
     * 管理员
     */
    private Admin admin;
    /**
     * 司机
     */
    private Driver driver;
    /**
     * 用户
     */
    private User user;
    /**
     * 用户类型
     */
    private Integer userType;

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public LoginUser(Admin admin) {
        this.admin = admin;
        this.userType = 0;
    }

    public LoginUser(Driver driver) {
        this.driver = driver;
        this.userType = 1;
    }

    public LoginUser(User user) {
        this.user = user;
        this.userType = 2;
    }

    public Driver getDriver() {
        return driver;
    }

    public void setDriver(Driver driver) {
        this.driver = driver;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        if (userType == 0) {
            return admin.getAdminPassword();
        } else if (userType == 1) {
            return driver.getPassword();
        } else if (userType == 2) {
            return user.getUserPassword();
        }
        return null;
    }

    @Override
    public String getUsername() {
        if (userType == 0) {
            return admin.getAdminUsername();
        } else if (userType == 1) {
            return driver.getUsername();
        } else if (userType == 2) {
            return user.getUserUsername();
        }
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return "LoginUser{" +
                "admin=" + admin +
                ", driver=" + driver +
                ", user=" + user +
                ", userType=" + userType +
                '}';
    }
}
